// pages/report/event_detail/event_detail.js
let app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    audioBtn: [{
      name: "播放",
      icon: "play",
      event: "playVoice"
    },
    {
      name: "暂停",
      icon: "pause",
      event: "pauseVoice"
    },
    {
      name: "停止",
      icon: "stop",
      event: "stopVoice"
    }
    ],
    isPlay: false,
    voice_people:""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      theme: app.globalData.theme,
      id: options.id
    });
    app.setNavigationBarColor();
    this.eventDetailLoad();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    this.innerAudioContext = wx.createInnerAudioContext();
    this.innerAudioContext.onEnded(() => {
      this.setData({
        isPlay: false
      });
    });
    this.innerAudioContext.onError((err) => {
      console.error(err);
      this.setData({
        isPlay: false
      });
      app.alert("温馨提示", "播放错误！")
    });
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    // this.innerAudioContext.destroy();
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  // 点击播放录音
  playVoice: function (e) {
    this.innerAudioContext.src = this.data.voice_people;
    this.innerAudioContext.play();
    this.setData({
      isPlay: true
    });
  },
  //停止播放
  stopVoice: function () {
    this.innerAudioContext.stop();
    this.setData({
      isPlay: false
    });
  },
  /**
   * 详情
   */
  eventDetailLoad: function () {
    app.wxGetRequest("/watch/selectEventDetailById", {
      eventId: this.data.id
    }).then(result => {
      let data = result.data;
      let enterAttachments = data.enterAttachments;
      let fileImgs = new Array();
      for (let i = 0; i < enterAttachments.length; i++) {
        fileImgs[fileImgs.length] = enterAttachments[i].attachment_url;
      }
      for (let i = 0; i < enterAttachments.length; i++) {
        if (enterAttachments[i].dic_type == '图片') {
          enterAttachments[i].dic_type = 1
        } else {
          this.setData({
            voice_people: enterAttachments[i].attachment_url
          });
          enterAttachments[i].dic_type = 2
        }
      }
      this.setData({
        fileImgs: fileImgs,
        event: data.eventDetails[0],
        record: data.eventFlow,
        attachment: enterAttachments,
        processedAttachment: data.processedAttachments,
      })
    });
  }
})